package com.example.activiti.demo.service;

import com.example.activiti.demo.dto.SysUserDto;
import com.example.activiti.demo.entity.SysUser;
import com.example.activiti.demo.util.ActivitiUserUtil;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.Picture;
import org.activiti.engine.identity.User;
import org.activiti.engine.identity.UserQuery;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityImpl;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: cheng
 * @date: 2020/2/21 17:01
 */
@Service
public class CustomUserEntityManager implements UserEntityManager, Session {

    @Resource
    private SysUserService sysUserService;


    @Override
    public User createNewUser(String s) {
        return null;
    }

    @Override
    public void updateUser(User user) {

    }

    @Override
    public List<User> findUserByQueryCriteria(UserQueryImpl userQuery, Page page) {
        SysUserDto userDto = new SysUserDto();
        userDto.setId(userQuery.getId());
        userDto.setRealName(userQuery.getLastName());
        userDto.setOrgId(userQuery.getGroupId());
        com.baomidou.mybatisplus.extension.plugins.pagination.Page page1 = new com.baomidou.mybatisplus.extension.plugins.pagination.Page();
        page1.setSize(page.getMaxResults());
        page1.setCurrent(page.getFirstResult() / page.getMaxResults() + 1);
        List<User> userList = new ArrayList<>();
        List<SysUser> sysUsers = sysUserService.querySysUserPage(userDto, page1);
        for (SysUser sysUser : sysUsers) {
            User user = new UserEntityImpl();
            user.setFirstName(sysUser.getUsername());
            user.setPassword(sysUser.getPassword());
            user.setLastName(sysUser.getRealName());
            user.setId(sysUser.getId());
            userList.add(user);
        }
        return userList;
    }


    @Override
    public List<Group> findGroupsByUser(String userId) {
        if (userId == null) {
            return null;
        }
        SysUser sysuser = sysUserService.getById(userId);
        //List<String> list = sysUserService.fingRoleCodeByUserId(userId);
        String[] split = sysuser.getRoleId().split(",");
        List<Group> list = new ArrayList<>();
        for (String id : split) {
            list.add(ActivitiUserUtil.toActivitiGroup(id));
        }
        return list;
    }

    @Override
    public UserQuery createNewUserQuery() {
        return null;
    }

    @Override
    public Boolean checkPassword(String s, String s1) {
        return true;
    }

    @Override
    public List<User> findUsersByNativeQuery(Map<String, Object> map, int i, int i1) {
        return null;
    }

    @Override
    public long findUserCountByQueryCriteria(UserQueryImpl query) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public long findUserCountByNativeQuery(Map<String, Object> parameterMap) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public boolean isNewUser(User user) {
        return false;
    }

    @Override
    public Picture getUserPicture(String s) {
        return null;
    }

    @Override
    public void setUserPicture(String s, Picture picture) {

    }

    @Override
    public void deletePicture(User user) {

    }

    @Override
    public void flush() {

    }

    @Override
    public void close() {

    }

    @Override
    public UserEntity create() {
        return null;
    }

    @Override
    public UserEntity findById(String s) {
        return null;
    }

    @Override
    public void insert(UserEntity entity) {

    }

    @Override
    public void insert(UserEntity entity, boolean b) {

    }

    @Override
    public UserEntity update(UserEntity entity) {
        return null;
    }

    @Override
    public UserEntity update(UserEntity entity, boolean b) {
        return null;
    }

    @Override
    public void delete(String s) {

    }

    @Override
    public void delete(UserEntity entity) {

    }

    @Override
    public void delete(UserEntity entity, boolean b) {

    }
}
